package org.nlp2rdf.navigator.util;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.nlp2rdf.navigator.client.common.ConceptDTO;
import org.nlp2rdf.navigator.client.common.ExampleDTO;
import org.nlp2rdf.navigator.client.common.LearnedConceptDTO;
import org.nlp2rdf.navigator.client.common.StoredConceptDTO;
import org.nlp2rdf.navigator.client.exception.NKEQueryException;
import org.nlp2rdf.navigator.component.resource.IResourceManager;

/**
 * A collection of Helpers for datatypes
 * 
 * @author Sebastian Hellmann <hellmann@informatik.uni-leipzig.de>
 * 
 */
public class DTOHelper {
	private static final Logger logger = Logger.getLogger(DTOHelper.class);

	public static Set<String> toStringSet(Collection<ExampleDTO> examples) {
		Set<String> set = new HashSet<String>();
		for (ExampleDTO exampleDTO : examples) {
			set.add(exampleDTO.getUri());
		}
		return set;
	}

	public static StoredConceptDTO makeStoredConcept(LearnedConceptDTO l) {
		StoredConceptDTO s = new StoredConceptDTO();

		Field[] f = l.getClass().getFields();
		for (Field from : f) {
			try {
				Field to = s.getClass().getField(from.getName());
				to.set(s, from.get(l));
			} catch (SecurityException e) {
				logger.error("doesn't seem to work", e);
			} catch (NoSuchFieldException e) {
				logger.error("doesn't seem to work", e);
			} catch (IllegalArgumentException e) {
				logger.error("doesn't seem to work", e);
			} catch (IllegalAccessException e) {
				logger.error("doesn't seem to work", e);
			}

		}
		return s;

	}

	/**
	 * Adds several things to the concepts 1. prefixes 2. comments
	 * 
	 * @param conceptDTO
	 * @param resourceManager
	 * @param prefixer
	 * @throws NKEQueryException
	 */
	public static void refineLearningResult(ConceptDTO conceptDTO, IResourceManager resourceManager,
			Prefixer prefixer) throws NKEQueryException {

		conceptDTO.setNamespaceToPrefix(prefixer.getNamespaceToPrefix());
		conceptDTO.setPrefixToNamespace(prefixer.getPrefixToNamespace());
		conceptDTO.setBase(prefixer.getBase());

		Map<String, String> comments = resourceManager.getConceptComments(conceptDTO
				.getNamedConceptsAndProperties());
		if (comments.size() == 0) {
			logger.warn("Empty comments for the following named concepts/individuals: "
					+ conceptDTO.getNamedConceptsAndProperties());
		}
		logger.debug("found " + comments.size() + " comments for "
				+ conceptDTO.getNamedConceptsAndProperties());

		if (conceptDTO instanceof LearnedConceptDTO) {
			LearnedConceptDTO l = (LearnedConceptDTO) conceptDTO;
			l.setConceptComments(comments);
			if (l instanceof StoredConceptDTO) {
				StoredConceptDTO s = (StoredConceptDTO) l;
			}
			// assigning labels to named classes from the ontology
		} else {
			String mos = conceptDTO.getManchesterSyntaxString();
			Map<String, String> map = resourceManager.getLabelsForURIs(new HashSet<String>(Arrays
					.asList(new String[] { mos })));
			if (map.size() > 1) {
				logger.warn("Found several labels for MOS string: " + mos + "\n" + map);
			}
			conceptDTO.setLabel(map.get(mos));
			if (map.get(mos) == null) {
				logger.warn("No label found for " + mos);
			}

		}

	}

}
